//
//  ResetPayViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/9/14.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "ResetPayViewController.h"

@interface ResetPayViewController (){
    NSInteger count;
}
@property (weak, nonatomic) IBOutlet UIButton *btnCode;
@property (weak, nonatomic) IBOutlet UIButton *btnSave;
@property (weak, nonatomic) IBOutlet UITextField *textPhone;
@property (weak, nonatomic) IBOutlet UITextField *textCode;
@property (weak, nonatomic) IBOutlet UITextField *textPwd;

@end

@implementation ResetPayViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self showNav:@"支付密码设置"];
    _btnSave.layer.borderWidth = 1.0;
    _btnSave.layer.borderColor = [UIColor redColor].CGColor;
    
    _btnCode.layer.borderWidth = 1.0;
    _btnCode.layer.borderColor = [UIColor redColor].CGColor;
    
}

- (IBAction)clickCode:(id)sender {
    if ([_textPhone.text isEqualToString:@""]) {
        [SVProgressHUD showErrorWithStatus:@"手机号不能为空"];
    }else if (_textPhone.text.length != 11){
        [SVProgressHUD showErrorWithStatus:@"手机号码必须为11位"];
    }else{
        
        NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
        [parmas setObject:_textPhone.text forKey:@"mobile"];
        [parmas setObject:@"8" forKey:@"k"];
        NSLog(@"parmas:%@",parmas);
        AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
        NSString *url = [NSString stringWithFormat:@"%@/App/Userlogin/CallSms",urlHead];
        [manager POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
            NSLog(@"res:%@",responseObject);
            NSString *str = [responseObject objectForKey:@"status"];
            if ([str integerValue] == 200) {
                [self performSelector:@selector(countClick) withObject:nil];
            }else{
                [SVProgressHUD showErrorWithStatus:[responseObject objectForKey:@"msg"]];
            }
            
        } failure:^(NSURLSessionDataTask *task, NSError *error) {
            NSLog(@"error:%@",error);
        }];
        
    }
    
}
//
-(void)countClick
{
    _btnCode.enabled = NO;
    count = 119;
    [_btnCode setTitle:@"119秒后重新获取" forState:UIControlStateNormal];
    
    [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(timerFired:) userInfo:nil repeats:YES];
}

- (void)timerFired:(NSTimer *)time{
    if (count !=1) {
        count -=1;
        
        NSString *str = [NSString stringWithFormat:@"%li",count];
        NSString *str2 = @"秒后重新获取";
        NSString *title = [NSString stringWithFormat:@"%@%@",str,str2];
        
        [_btnCode setTitle:title forState:UIControlStateNormal];
    }else{
        [time invalidate];//关闭定时器
        _btnCode.enabled = YES;
        [_btnCode setTitle:@"获取验证码" forState:UIControlStateNormal];
    }
    
}
- (IBAction)clickSave:(id)sender {
    if (_textPhone.text.length != 11) {
        [self showAlert:@"手机号必须为11位"];
        return;
    }else if (_textCode.text.length == 0){
        [self showAlert:@"验证码不能为空"];
        return;
    }else if (_textPwd.text.length < 6 && _textPwd.text.length > 12){
        [self showAlert:@"密码长度不够"];
        return;
    }
    
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    [parmas setObject:_textPwd.text forKey:@"newPaymentPassword"];
    [parmas setObject:_textCode.text forKey:@"smscode"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/editPaymentPassword",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"res:%@",responseObject);
        
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        NSString *mess = [responseObject objectForKey:@"msg"];
        if ([state integerValue] == 200) {
            [self alertBack];
        }else{
            [self showAlert:mess];//错误信息
        }
        
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
    
}
//返回
- (void)alertBack{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"修改成功" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        [self.navigationController popViewControllerAnimated:YES];
    }];
    
    [alert addAction:action];
    [self presentViewController:alert animated:YES completion:nil];
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [[UIApplication sharedApplication].keyWindow endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
